import config
import os
import re
import time
import urllib.request
from recorder import recorder
from utils import utils

class part1:

    def __init__(self):
        """
        part1 info
        * down search result html to get books index url link
        """
    #----------------------------------------------------------------------
    
    def run(self):
        bookList = []
        bookList = self.getBookIndexUrl()
        self.getAllBookUrl(bookList)
        print('part 1 end!')

    def getBookIndexUrl(self):
        bengouBookList = set()
        for letter in config.letterList :
            letterIndexUrl = config.searchTargetUrl % tuple([letter,1])
            contect = utils.getHtmlFromUrl(letterIndexUrl)
            searchTargetRule = config.ruleSearchTarget % letter
            pageMax = self.getMaxPageNumber(contect,ruleSearchTarget)
            print('letter : %s ; page max : %d %s' % tuple([letter,pageMax,utils.getLocationTime()]))
            for page in list(range(1,pageMax+1)):
                letterPageUrl = config.searchTargetUrl % tuple([letter,page])
                connectPage = utils.getHtmlFromUrl(letterPageUrl)
                bengouBookList = bengouBookList.union(set(re.findall(config.ruleBookIndex,connectPage)))
        bengouBookListLog = recorder.readLogToList(config.filePathBengouBookIndexLog)
        for url in bengouBookList :
            bengouBookListLog = bengouBookListLog + [("%s%s" % tuple([config.domain,url]))]
        recorder.wirteLog(config.filePathBengouBookIndexLog,set(bengouBookListLog))
        return bengouBookListLog
        
    
    #----------------------------------------------------------------------
                
    def getMaxPageNumber(self,contect,ruleSearchTarget):
        pageList = set(re.findall(ruleSearchTarget,contect))
        if(len(pageList)==0):
            page = 0
        else:
            page = max(pageList)     
        return int(page)
        
    #----------------------------------------------------------------------
    
    def getAllBookUrl(self,bookList=[]):
        if(len(bookList) == 0):
            bookList = recorder.readLogToList(config.bengouBookIndexLog)
        bookList = set(bookList)
        for url in bookList :
            splitUrl = re.match(config.ruleSplitBookIndex,url)
            logFilePath = (config.filePathBookIndexUrlLog,tuple([splitUrl.group(0),splitUrl.group(1)]))
            
            if(os.path.exists(logFilePath)):
                print('file path exists : ',logFilePath,'',utils.getLocationTime())
                print('end url : ',splitUrl,'',utils.getLocationTime())
                continue
            bookUrlList = []
            m = re.match(self.ruleHostUrl,url)
            currentUrl = m.group(0)
            content = utils.getHtmlFromUrl(url)
            if(len(content) > 0) : 
                AllBookList = re.findall(config.ruleAllBookIndex,content)
                for id1,id2,name in allBookList:
                    bookUrlList = bookUrlList + ["%s%s/%s.html%s%s" % tuple([currentUrl,id1,id2,config.splitLogString,name])]
                recorder.wirteLog(logFilePath,set(bookUrlList))    
    
    def __del__(self):
        pass